
 /**  
 * Project Name:zhonghecrm  
 * File Name:SysMenuController.java  
 * Package Name:com.crm.controller  
 * Date:2016年10月26日下午2:19:38   
 *  
*/  
  
package com.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.SysMenu;
import com.crm.service.SysMenuService;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

/**  
 * ClassName:SysMenuController <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月26日 下午2:19:38 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Controller
@RequestMapping("/menu")
public class SysMenuController {
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@RequestMapping("/index")
	public String index(Page<SysMenu> page, SysMenu t, ModelMap map){
		
		t.setEnable(null);
		List<SysMenu> list = (List<SysMenu>) sysMenuService.selectAll(page, t);
		
		map.put("list", list);
		
		return "sysconf/menu";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getAll(Page<SysMenu> page, SysMenu record){
		Map<String, Object> map = new HashMap<>();
		
		Page<SysMenu> list = (Page<SysMenu>)sysMenuService.selectAll(page);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "获取成功！");
		
		return map;
	}
	
	@RequestMapping("getSiblings")
	@ResponseBody
	public Map<String, Object> getSiblings(Page<SysMenu> page, SysMenu t){
		Map<String, Object> map = new HashMap<>();
		
		t = sysMenuService.selectByPrimaryKey(t.getMenuId());
		
		SysMenu sysMenu = new SysMenu();
		if(ValidateUtil.isEmpty(t.getParentId())){
			sysMenu.setParentId("0");
		}else{
			sysMenu.setParentId(t.getParentId());
		}
		
		List<SysMenu> list = sysMenuService.selectForSort(page, sysMenu);
		
		for(SysMenu e : list){
			System.out.print(e.equals(t));
		}
		
		list.remove(t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(SysMenu record){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(record.getFullName()))
			throw new RuntimeException("名称不能为空！");
		
		sysMenuService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(SysMenu record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		sysMenuService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(SysMenu record){
		
		Map<String, Object> map = new HashMap<>();
		
		sysMenuService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	
	 /**  
	 * orderMenu:(对菜单进行排序). <br/>  
	 *  
	 * @author hanlinxiu  
	 * @param record
	 * @param refMenuId
	 * @param order 前-1，后-2
	 * @return  
	 * @since JDK 1.8  
	 */
	@RequestMapping("/sortMenu")
	@ResponseBody
	public Map<String, Object> sortMenu(SysMenu record, String refMenuId, Integer order){
		
		Map<String, Object> map = new HashMap<>();
		
		sysMenuService.sortMenu(record, refMenuId, order);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
}
  
